package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boot.entity.Menu;
import com.boot.mapper.MenuMapper;
import com.boot.mapper.RoleMenuMapper;
import com.boot.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shuchong
 * @since 2022-07-07
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> findMenus(String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }
        //查询所有数据
        List<Menu> list = list(queryWrapper);
        //找出pid为null的一级菜单
        List<Menu> parentNodes = list.stream().filter(menu->menu.getPid()==null).collect(Collectors.toList());
        parentNodes.forEach(menu -> {
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        });
        return parentNodes;
    }

    @Override
    public List<Menu> findMenusById(String roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",roleId);
        Menu menu = menuMapper.selectOne(queryWrapper);

        return null;
    }
}
